package com.sanchunhui.psc.presenter

import android.util.JsonToken
import android.util.Log
import com.sanchuihui.presenter.view.LoginView
import com.sanchunhui.psc.data.protocol.ResponseInfo
import com.sanchunhui.psc.data.protocol.TokenReq
import com.sanchunhui.psc.ext.excute
import com.sanchunhui.psc.presenter.view.LogoutView
import com.sanchunhui.psc.rx.BaseSubscriber
import com.sanchunhui.psc.service.UserService
import com.sanchunhui.psc.ui.activity.SettingActivity
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import javax.inject.Inject

/**
 * @Author zyx
 * @Date 2021/6/17-10:02
 * @Dscribe
 */
class LogoutPresenter @Inject constructor() : BasePresenter<LogoutView>(){

    @Inject
    lateinit var userService: UserService

    /*
        注销登录
     */
    fun logout(token: String) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        userService.logout(token).excute(object : BaseSubscriber<Boolean>(mView) {
            override fun onNext(t: Boolean) {
                mView.onGetLogoutResult(t)
            }
        }, lifecycleProvider)

    }

    /*
    // 使用手机号码登录的业务
    fun logout(tokenStr: String) {
        val tokenReq = TokenReq(tokenStr)
        //TODO:要异步访问
        val observable: Observable<ResponseInfo> = pscService.logout(tokenReq)
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if (it.code == 0) {
                    settingActivity.onLoginSuccess()
                } else {
                    settingActivity.onLoginFailed()
                }
            },{
                Log.e("rxjava",it.localizedMessage)
            },{
                Log.e("rxjava","onComplete!")
            })
    }

    fun getUserInfo(tokenStr: String) {

        //TODO:要异步访问
        val observable: Observable<ResponseInfo> = pscService.getUserInfo(tokenStr)
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if (it.code == 0) {
                    settingActivity.onLoginSuccess()
                } else {
                    settingActivity.onLoginFailed()
                }
            },{
                // 错误信息
                Log.e("rxjava",it.localizedMessage)

            },{
                Log.e("rxjava","onComplete!")
            })
    }
    */
}